package plus;

import java.util.*;

public class Identifier {
	public final int lineNr;
	public final String name;
	public final String scope;
	public Identifier(int l, String name, String scope) { lineNr = l; this.name = name; this.scope = scope; }
	public Module where() { return Uni.need_module(scope, lineNr); }
	public boolean isLocal() { return scope == null; }
	public Symbol<TypeSym> asType() {
		return isLocal() ? Uni.findType(name) : where().types.find(name);
	}
	public Symbol<Variable> asVar() {
		if (isLocal()) return VScope.current.find_variable(name);
		//else return where().find_variable(name);
		else throw new Gripe(lineNr, "Use of module-scope vars is unimplemented.");
	}
	public String toString() {
		return Integer.toString(lineNr)+": "+
				(isLocal() ? name : name + "@" + scope);
	}
	public void functionCandidates(int length, Collection<Symbol<Function>> candidates) {
		if (isLocal()) {
			Uni.findFunction(name, length, candidates);
		} else {
			where().fe.find_function(name, length, candidates);
		}
	}
	public void gripe(String what) { Gripe.gripe(lineNr, name+": "+what); }
	public Symbol<Variable> addVar() {
		if (isLocal()) return VScope.current.add_local(name, lineNr);
		else throw new Gripe(lineNr, "Use of module-scope vars is unimplemented.");
	}
}
